package chenji.itemeditor;

import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import chenji.itemeditor.editor.CommonEditor;
import chenji.itemeditor.editor.TellrawEditor;
import chenji.itemeditor.util.UpdateManager;
import chenji.itemeditor.util.asmtool.ASMToolManager;
import chenji.itemeditor.util.asmtool.ReflectTool;

public class ItemEditorAPI {
	public final static ItemEditorAPI INSTANCE = new ItemEditorAPI();
	private UpdateManager update;
	private Plugin plugin;
	private ASMToolManager asmtoolmanager;
	private Logger logger;

	public void setPlugin(Plugin plugin) {
		this.plugin = plugin;
		this.initialize();
	}

	
	public void initialize() {
		this.logger = plugin.getLogger();
		asmtoolmanager = new ASMToolManager();
		this.loadEditor();
		this.update=new UpdateManager();
	}
	public Plugin getPlugin() {
		return this.plugin;
	}
	
	public UpdateManager getUpdateManager(){
		return this.update;
	}

	public Logger getLogger() {
		return this.logger;
	}

	public static ItemEditorAPI getInstance() {
		return INSTANCE;
	}

	public ASMToolManager getASMToolManager() {
		return this.asmtoolmanager;
	}

	public void disable() {
		Bukkit.getPluginManager().disablePlugin(this.plugin);
	}

	public void loadEditor() {
		if (ReflectTool.isSpigot()) {
			this.loadEditor(new TellrawEditor());
			return;
		} else {
			this.loadEditor(new CommonEditor());
			return;
		}
	}

	public void loadEditor(CommandExecutor executor) {
		JavaPlugin p = (JavaPlugin) plugin;
		p.getCommand("ItemEditor").setExecutor(executor);
		p.getCommand("ie").setExecutor(executor);
		p.getCommand("ieTellraw").setExecutor(executor);
	}

}
